#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
struct ListNode 
{
    int val;
    struct ListNode* next;
};
typedef struct ListNode LNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    LNode* pl1 = list1;
    LNode* pl2 = list2;
    if (pl1 == NULL)
    {
        return pl2;
    }
    if (pl2 == NULL)
    {
        return pl1;
    }
    if (pl1->val > pl2->val)
    {
        LNode* tmp = pl1;
        pl1 = pl2;
        pl2 = tmp;
    }
    LNode* new = pl1;
    LNode* ptail = new;
    pl1 = pl1->next;
    while (pl1 != NULL && pl2 != NULL)
    {
        if (pl1->val < pl2->val)
        {
            ptail->next = pl1;
            ptail = pl1;
            pl1 = pl1->next;
        }
        else
        {
            ptail->next = pl2;
            ptail = pl2;
            pl2 = pl2->next;
        }
    }
    if (pl1 == NULL)
    {
        ptail->next = pl2;
    }
    else
    {
        ptail->next = pl1;
    }
    return new;
}